package cn.ctyun.ipush.controller;

import cn.ctyun.ipush.constant.ResultInfo;
import cn.ctyun.ipush.dto.ResultDto;
import cn.ctyun.ipush.model.HbbtProductModel;
import cn.ctyun.ipush.service.ProdService;
import cn.ctyun.ipush.utils.ConfigUtils;
import cn.ctyun.ipush.utils.DBUtils;
import cn.ctyun.ipush.utils.DataUtils;
import cn.ctyun.ipush.web.user.bo.UserModel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yanghongbo on 2017/5/10.
 * 功能：用于对产品相关界面做control操作
 *
 */
@Controller
public class ProductControl {

    @Autowired
    private ProdService prodService;

    @RequestMapping(value = "productGLYIntoPage", method = RequestMethod.GET)
    public ModelAndView productGLYIntoPage(HttpSession session, HttpServletRequest request) {
        /**
         * productInputMsg 需要返回当前用户的信息
         */

        try {
            Object o = session.getAttribute("userInfo");
            if (o != null) {
                String sql="select *  from  hbbt_product_model where is_deleted='0' ";
                List list= DBUtils.executeFreeSelectBySql(ConfigUtils.getStringValue("jdbcUrl"), ConfigUtils.getStringValue("username"), ConfigUtils.getStringValue("password"), sql);
                Map<String, Object> data = new HashMap<String, Object>();
                data.put("productList", list);
                return new ModelAndView("IB_GLY_MANAGEPRODUCT", data);

            }
            return new ModelAndView("IB_GLY_MANAGEPRODUCT", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelAndView("IB_GLY_MANAGEPRODUCT", null);
        }
    }


    @RequestMapping(value = "alterProductBonus", method = RequestMethod.POST)
    public ModelAndView alterProductBonus(HttpServletRequest request, HttpSession session) {

        String id = request.getParameter("prodId");
        String reward=request.getParameter("alterBonusNumber");
        try {
            HbbtProductModel  hbbtProductModel=prodService.querySingleResult(id);
            hbbtProductModel.setProdReward(reward);
            prodService.update(hbbtProductModel);
            String sql="select *  from  hbbt_product_model where is_deleted='0' ";
            List list= DBUtils.executeFreeSelectBySql(ConfigUtils.getStringValue("jdbcUrl"), ConfigUtils.getStringValue("username"), ConfigUtils.getStringValue("password"), sql);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("productList", list);
            return new ModelAndView("IB_GLY_MANAGEPRODUCT", data);
        } catch (Exception ex) {
            return new ModelAndView("IB_GLY_MANAGEPRODUCT", null);
        }
    }

    @RequestMapping(value = "lookProdinfo", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto lookProdinfo(HttpServletRequest request,HttpSession session) {
        ResultDto resultDto = new ResultDto();
        String requestJsonParam = DataUtils.getJsonFromRequest(request);
        JSONObject jsonObject = JSON.parseObject(requestJsonParam);
        String id = jsonObject.getString("id");
        try {
            UserModel o = (UserModel) session.getAttribute("userInfo");
            if (o != null) {
                String sql = String.format("select * from hbbt_product_model where hbbt_product_model_id='%s' ", id);
                List list = DBUtils.executeFreeSelectBySql(ConfigUtils.getStringValue("jdbcUrl"), ConfigUtils.getStringValue("username"), ConfigUtils.getStringValue("password"), sql);
                return resultDto.setStatus(ResultInfo.success).setInfo(list);
            } else {
                return resultDto.setStatus(ResultInfo.success).setInfo("失败");
            }
        } catch (Exception e) {
            return resultDto.setStatus(ResultInfo.failure).setInfo("失败");
        }
    }

    //alterProductBonus

}
